<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>default-event-rule-update</title>
    <%@ include file="../include/common.jsp"%>
    <%@ include file="../include/jbox.jsp"%>
    <%@ include file="../include/form.jsp"%>
	<%@ include file="../include/crontab.jsp"%>
    <%@ include file="../include/timeUnit.jsp"%>
    
    <script type="text/javascript">
    var stu;
	var ctd;
	var expr = '';
   	function back(){
		window.history.back();
	}
	$(function(){
		if('${defaultEventRule.vailTime}' != '' && '${defaultEventRule.vailTime}' !='0'){
			stu.reversal('${defaultEventRule.vailTime}'.split('@')[1]);
			ctd.reversal('${defaultEventRule.vailTime}'.split('@')[0]);
			$("#vailTimeCheckbox").attr("checked", true);
			vailTimeCheckboxClick();
		}
		if('${defaultEventRule.confirmDuration}' != ''){
			tunit.reversal('${defaultEventRule.confirmDuration}');
		}
		if('${defaultEventRule.filterDuration}' != ''){
			funit.reversal('${defaultEventRule.filterDuration}');
		}
		if('${defaultEventRule.duration}' == ''){
			$("#duration").val("0");
		}
		$("input[type=radio][value="+'${defaultEventRule.level}'+"]").attr("checked",'checked'); 
		
		$('#expr').live('blur',function(){
			expr = $("#expr").val();
			if(expr != '' && (expr.indexOf("TP") >= 0 || expr.indexOf("tprop") >= 0)){
				$("#keyTr").css({"display":""});
			}else{
				$("#keyTr").css({"display":"none"});
				$("#objectExpr").val('');
			}
		});
		$('#expr').blur();
		
		$.formValidator.initConfig({formid:"myform"
	          ,onerror:function(msg){ }
	          ,onsuccess:function(){
	        	  	add();
	          		return false;
	          }
	    });
		$("#msg").formValidator({onshow:"不能为空",onfocus:"不能为空",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:256,onerror:"长度非法"});
		$("#expr").formValidator({onshow:"不能为空",onfocus:"不能为空",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:256,onerror:"长度非法"});
		$("#duration").formValidator({onshow:"请输入数字 长度(1-6)",onfocus:"请输入数字 长度(1-6)",oncorrect:"输入正确"}).inputValidator({min:1,onerror:"不能为空"}).inputValidator({min:1,max:5,onerror:"长度范围（1-6）"})
		  .functionValidator({
				    fun:function(val,elem){
				    	 var reg = /^[0-9]+$/;
				    	    if(!reg.test(val)){
				    	        return "格式不正确";
				    	    }
				    	    return true;
					}
		});
		$("#remarks").formValidator({onshow:"请输入字符串 长度(1-1024)",onfocus:"请输入字符串 长度(1-1024)",oncorrect:"输入正确"})
		.functionValidator({
		    fun:function(val,elem){
		    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 1024){
		    	        return "长度超出最大范围";
		    	    }
		    	    return true;
			}
 		});
		$("#keyWord").formValidator({onshow:"请输入字符串 长度(1-1024)",onfocus:"请输入字符串 长度(1-1024)",oncorrect:"输入正确"})
		.functionValidator({
		    fun:function(val,elem){
		    	    if(val.replace(/[^\x00-\xff]/g, "xx").length > 1024){
		    	        return "长度超出最大范围";
		    	    }
		    	    return true;
			}
 		});
	})
	function vailTimeCheckboxClick(){
		var str=document.getElementById("vailTimeCheckbox");
		if(str.checked == true){
			$("#vailTimeTr").css({"display":""});
			$("#vailTimeTr1").css({"display":""});
		}else{
			$("#vailTimeTr").css({"display":"none"});
			$("#vailTimeTr1").css({"display":"none"});
		}
	}
	function addMsg(){
		var url ="iframe:event/default-event-rule!editExprMsg.action?msg="+encodeURI(encodeURI($("#msg").val()))+"&flag=msg"+"&id="+$("#id").val();
		var opt = { title: "修改默认事件消息",width:580,height:360,buttons: {}};
		getTopWin(window).openjBox(url,opt,'default-event-rule-addExprMsg',window);
	}
	function addExpr(){
		var url ="iframe:event/default-event-rule!editExprMsg.action?expr="+encodeURI(encodeURI($("#expr").val()))+"&flag=expr"+"&id="+$("#id").val();
		var opt = { title: "修改默认事件表达式",width:580,height:360,buttons: {}};
		getTopWin(window).openjBox(url,opt,'default-event-rule-addExprMsg',window);
	}
	function addExprOrMsgValue(exprOrMsg,flag){
		if(flag=='expr'){
			$("#expr").val(exprOrMsg);
			$("#expr").blur();
		}else if(flag=='msg'){
			$("#msg").val(exprOrMsg);
		}
	}
	function add(){
		var vailTime = '';
		var str=document.getElementById("vailTimeCheckbox");
		if(str.checked == false){
			vailTime = '0';//全天
		}else{
			var arrays = (ctd.results()).split(" ");
	        if ("?" == arrays[5]) {
	            if ("*" == arrays[3]) {
	            } else {
	                // 月暂停
	            	if(stu.results() > 3600*24*31){
	            		getTopWin(window).getMessageBox().alert("有效时间选择按月后，持续时间不能超过一个月","提示");
	            		return;
		            }
	            }
	        } else {
	            // 说明是按星期
	            if(stu.results() > 3600*24*7){
	            	getTopWin(window).getMessageBox().alert("有效时间选择按周后，持续时间不能超过一周","提示");
	            	return;
	            }
	        }
			vailTime = ctd.results()+'@'+stu.results();
		}
		if(expr != '' && (expr.indexOf("TP") >= 0 || expr.indexOf("tprop") >= 0)){
			if($("#objectExpr").val() == ''){
				getTopWin(window).getMessageBox().alert("事件表达式key值不能为空","提示");
            	return;
			};
		}
		  //提交
		 $.ajax({
			   type: "post", 
			   url: "${pageContext.request.contextPath}/event/default-event-rule!save.action?vailTime="+vailTime+"&confirmDuration="+tunit.results()+"&filterDuration="+funit.results(), //请求的action
			   data: $("#myform").serialize(), //传的参数
			   dataType: "json",
			   success: function(data){ //结果
					window.history.back();
			   }
		});
	}
    </script>
  </head>
  
  <body>
    	<h2 class="title-h2"><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>修改默认事件规则</h2>
    	<form action="" id="myform" name="myform" method="post">
    		<table class="table_eidt" >
    			<tr>
    				<th class="td_left" width="150px">
    					<label>资源类别</label>
    				</th>
    				<td class="td_right">
    					<input type="hidden" id="resTypeId" name="resTypeId" value="${defaultEventRule.resTypeId}"></input>
	    				<label>${resTypeName}</label>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left" width="150px">
    					<label>事件类别</label>
    				</th>
    				<td class="td_right">
    					<input type="hidden" id="eventTypeId" name="eventTypeId" value="${defaultEventRule.eventTypeId}"></input>
	    				<label>${eventTypeName}</label>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<label>事件级别</label>
    				</th>
    				<td class="td_right">
    					<input type="radio" name="level"  value="2" ><label style="color: #C8FF00">轻微</label>
						<input type="radio" name="level"  value="3" ><label style="color: #FFD200">警告</label>
						<input type="radio" name="level"  value="4" ><label style="color: #FF9200">重要</label>
						<input type="radio" name="level"  value="5" ><label style="color: #FF1042">严重</label>
    				</td>
    			</tr>
    			<tr height="80px">
    				<th class="td_left">
    					<span class="span_alert">*</span><label>事件表达式</label>
    				</th>
    				<td class="td_right">
    					<textarea style="width: 600px;height: 50px;" id="expr" name="expr" onclick="addExpr();">${expr}</textarea>
    					<span id="exprTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr style="display: none;" id="keyTr">
    				<th class="td_left">
    					<span class="span_alert">*</span><label>事件表达式KEY值</label>
    				</th>
    				<td class="td_right">
    					<input type="text" id="objectExpr" name="objectExpr" style="width: 500px;" value='${defaultEventRule.objectExpr}'>
    					<span id="keyTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<label>事件阀值持续时间(秒)</label>
    				</th>
    				<td class="td_right">
    					<input type="text" id="duration" name="duration" value="${defaultEventRule.duration}" style="width: 60px;">
    					<span id="durationTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
	    			<td class="td_left">自动确认间隔时间</td>
	    			<td class="tdValue">
	    			<div id="divDuration"></div>
	    				<script type="text/javascript">
	    				 	tunit = new SimpleTimeUnit("divDuration","ddHHmm");
	    				 	tunit.show();
				        </script>
	    			</td>
    			</tr>
    			<tr>
	    			<td class="td_left">事件过滤持续时间</td>
	    			<td class="tdValue">
	    			<div id="divDurationFit"></div>
	    				<script type="text/javascript">
	    				 	funit = new SimpleTimeUnit("divDurationFit","ddHHmm");
	    				 	funit.show();
				        </script>
	    			</td>
    			</tr>
    			<tr>
	    			<td class="td_left">过滤内容</td>
	    			<td class="td_right"><textarea id="keyWord" name="keyWord" style="width: 600px;height: 50px">${defaultEventRule.keyWord}</textarea>
	    			<span id="keyWordTip" style="width:150px"></span></td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<input type="checkbox" id="vailTimeCheckbox" value="vailTimeCheckbox" onclick="vailTimeCheckboxClick();">
    					<label>指定有效时间段</label>
    				</th>
    				<td class="td_right">
    					<label>默认为全天</label>
    				</td>
    			</tr>
    			<tr style="display: none;" id="vailTimeTr">
	    				<th class="td_left">
	    					<label>有效时间</label>
	    				</th>
	    				<td class="tdValue">
							<div id="vailTime"></div>
							<script>
					       		ctd = new Crontabs("vailTime");
					       	 	ctd.show();
					         </script>
	    				</td>
	    		</tr>
	    		<tr style="display: none;" id="vailTimeTr1">
	    				<th class="td_left">
	    					<label>持续时间</label>
	    				</th>
	    				<td class="tdValue">
							<div id="vailTime1"></div>
							<script type="text/javascript">
								stu = new SimpleTimeUnit("vailTime1","ddHHmm");
								stu.show();
		    			        ctd.setTU(stu);
					        </script>
	    				</td>
	    		</tr>
    			<tr height="80px">
    				<th class="td_left">
    					<span class="span_alert">*</span><label>事件消息</label>
    				</th>
    				<td class="td_right">
	    				<textarea style="width: 600px;height: 50px;" id="msg" name="msg" onclick="addMsg();">${msg}</textarea>
    					<span id="msgTip" style="width:200px"></span>
    				</td>
    			</tr>
    			<tr>
    				<th class="td_left">
    					<label>规则注释</label>
    				</th>
    				<td class="td_right">
    					<textarea style="width: 600px;height: 50px;" id="remarks" name="remarks">${defaultEventRule.remarks}</textarea>
    					<span id="remarksTip" style="width:200px"></span>
    				</td>
    			</tr>
    		</table>
    		<input type="hidden" id="id" name="id" value="${defaultEventRule.id}"/>
    		<input type="hidden" id="status" name="status" value="${defaultEventRule.status}"/>
    		<div style="margin-left:150px; margin-top:5px;">
		    	<button type="submit" id="button" name="button" class="btn-4">保存</button>
		    	<button type="button" onclick="back();" class="btn-4">返回</button>
    		</div>
    	</form>
  </body>
</html>
